package com.liupaik.pennmarketplace;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.Toast;

public class TabMain extends TabActivity {
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.tabmain);

	    Resources res = getResources(); // Resource object to get Drawables
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, BooksGroup.class);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec("books").setIndicator("Books",
	                      res.getDrawable(R.drawable.books))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    // Do the same for the other tabs
	    intent = new Intent().setClass(this, FurnitureGroup.class);
	    spec = tabHost.newTabSpec("furniture").setIndicator("Furniture",
	                      res.getDrawable(R.drawable.furniture))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    intent = new Intent().setClass(this, SubletsGroup.class);
	    spec = tabHost.newTabSpec("sublets").setIndicator("Sublets",
	                      res.getDrawable(R.drawable.sublets))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    
	    intent = new Intent().setClass(this, MiscGroup.class);
	    spec = tabHost.newTabSpec("misc").setIndicator("Misc",
	                      res.getDrawable(R.drawable.misc))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    
	    intent = new Intent().setClass(this, MyItemsTab.class);
	    spec = tabHost.newTabSpec("myitems").setIndicator("My Items",
	                      res.getDrawable(R.drawable.myitems))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    

	    
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menuwithsell, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	
    	case R.id.sellmenu:
    		//finish();
    		startActivity(new Intent(this, Sell.class));
    		return true;
    	
		case R.id.preferences:
			startActivity(new Intent(this, Prefs.class));
			return true; 
			
		case R.id.logout:
			//IMPLEMENT LOGOUT HERE
			
			
			Toast.makeText(
					getApplicationContext(),
					"Successfully Logged Out!",
					Toast.LENGTH_SHORT)
					.show();
			Intent myIntent = new Intent(TabMain.this, Intro.class);
			startActivityForResult(myIntent, 0);
			
//			new AlertDialog.Builder(TabMain.this)
//			//.setTitle(Register.this.getResources().getString(R.string.register_alert))  
//			.setMessage("Successfully Logged Out!")  
//			//.setIcon(R.drawable.book)  
//			.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, int id) {
//					Intent myIntent = new Intent(TabMain.this, Intro.class);
//					startActivityForResult(myIntent, 0);
//				}
//			})
//			.show();
			
			return true;
    	}
    	return false;
    }
    
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent i = new Intent(TabMain.this, BuySell.class);
            startActivityForResult(i, 0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    
    
}
